package com.xindaoapp.happypet.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.bean.NearFriends.Friend;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.view.CircleImageView;

public class NearFriendAdapter extends BaseAdapter {
    private final Activity mContext;
    private final List<Friend> arrays = new ArrayList<Friend>();

    public NearFriendAdapter(Activity mContext) {
        super();
        this.mContext = mContext;
    }

    public void clearItems() {
        arrays.clear();
    }

    public void addItems(List<Friend> items) {
        arrays.addAll(items);
    }

    public List<Friend> getAll() {
        return arrays;
    }

    @Override
    public int getCount() {
        return arrays.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Friend friend = arrays.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.nearfriend_list_item, null);
            holder.pic = (CircleImageView) convertView.findViewById(R.id.pic);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.near = (TextView) convertView.findViewById(R.id.near);
            holder.pet = (TextView) convertView.findViewById(R.id.pet);
            holder.attention = (ImageView) convertView.findViewById(R.id.attention);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(friend.userface, holder.pic);
        holder.name.setText(friend.username);
        float distance = Float.valueOf(friend.distance);
        if (distance >= 1000) {
            distance = distance / 1000;
            holder.near.setText("距离" + formatDistance(distance) + "千米");
        } else {
            holder.near.setText("距离" + (int) distance + "米");
        }
        if (friend.isfollow.equals("0")) {
            holder.attention.setImageResource(R.drawable.other_guanzhu);
        } else if (friend.isfollow.equals("1")) {
            holder.attention.setImageResource(R.drawable.guanzhu_ok);
        }
        holder.attention.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserState.isLogged(mContext)) {
                    if (friend.isfollow.equals("0")) {
                        RequestParams params = new RequestParams();
                        params.put("follow_uid", friend.uid);
                        params.put("follow_username", friend.username);
                        AddClient.get(Address.GUANZHUOTHER, params, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, String content) {
                                try {
                                    BaseEntity parse = JSON.parseObject(content, BaseEntity.class);
                                    if (parse.getResult().equals("0")) {
                                        friend.isfollow = "1";
                                        notifyDataSetChanged();
                                        holder.attention.setImageResource(R.drawable.guanzhu_ok);
                                        Toast.makeText(mContext, "关注成功", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(mContext, "关注失败", Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception e) {
                                    notifyDataSetChanged();
                                    Toast.makeText(mContext, "已关注", Toast.LENGTH_SHORT).show();
                                }
                                super.onSuccess(statusCode, content);
                            }

                            @Override
                            public void onFailure(Throwable error, String content) {
                                super.onFailure(error, content);
                            }
                        });
                    }
                } else {
                    RequestParams params = new RequestParams();
                    params.put("follow_uid", friend.uid);
                    AddClient.get(Address.GUZNZHUDELETE, params, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, String content) {
                            try {
                                friend.isfollow = "0";
                                notifyDataSetChanged();
                                holder.attention.setImageResource(R.drawable.other_guanzhu);
                                Toast.makeText(mContext, "取消关注成功", Toast.LENGTH_SHORT).show();
                                super.onSuccess(statusCode, content);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Throwable error, String content) {
                            Toast.makeText(mContext, "关注失败", Toast.LENGTH_SHORT).show();
                            super.onFailure(error, content);
                        }
                    });
                }
            }
        });
        holder.pet.setText(friend.petbreed);

        return convertView;
    }

    class ViewHolder {
        CircleImageView pic;
        TextView name;
        TextView near;
        ImageView attention;
        TextView pet;
    }

    private String formatDistance(Float a) {
        DecimalFormat df = new DecimalFormat("0.0");
        return df.format(a);
    }
}
